<template>
  <div>
    <div class="layui-container fly-marginTop">
      <div class="fly-panel fly-panel-user" pad20>
        <div class="layui-tab layui-tab-brief" lay-filter="user">
          <ul class="layui-tab-title">
            <li><router-link to="/login">登入</router-link></li>
            <li class="layui-this">注册</li>
          </ul>
          <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
            <div class="layui-tab-item layui-show">
              <div class="layui-form layui-form-pane">
                
                  <div class="layui-form-item">
                    <label for="L_email" class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                      <input v-model="email" type="text" id="L_email" name="email" required lay-verify="email" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-word-aux">
                      <button v-if="showSendMail" type="button" @click="sendMail" class="layui-btn">发送邮件</button>
                      将会成为您唯一的登入名
                    </div>
                  </div>

                <div class="layui-form-item" v-if="showMailCode">
                  <label for="L_vercode" class="layui-form-label">邮箱验证码</label>
                  <div class="layui-input-inline">
                    <input v-model="code" type="text" name="code" required lay-verify="required" autocomplete="off" class="layui-input">
                  </div>
                </div>

                  <div class="layui-form-item">
                    <label for="L_username" class="layui-form-label">昵称</label>
                    <div class="layui-input-inline">
                      <input v-model="name" type="text" id="L_username" name="username" required lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label for="L_pass" class="layui-form-label">密码</label>
                    <div class="layui-input-inline">
                      <input v-model="password" type="password" id="L_pass" name="pass" required lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">6到16个字符</div>
                  </div>
                  <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label">确认密码</label>
                    <div class="layui-input-inline">
                      <input v-model="confirmPassword" type="password" id="L_repass" name="repass" required lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label for="L_vercode" class="layui-form-label">人类验证</label>
                    <div class="layui-input-inline">
                      <input v-model="vercode" type="text" id="L_vercode" name="vercode" required lay-verify="required" placeholder="请回答后面的问题" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">
                      <span style="color: #c00;">{{one}} + {{two}} = ?</span>
                    </div>
                  </div>
                  <div>
                    <button @click="submitForm" class="layui-btn">立即注册</button>
                  </div>

              </div>
            </div>
          </div>
        </div>
      </div>
  </div>
  </div>
</template>

<script>
import { message } from '@/element/message';
import { register, sendRegisterMail } from '@/api/login';
export default {
  name: 'RegisterReg',
  data () {
    return {
      email: '',
      code: '',
      name: '',
      password: '',
      confirmPassword: '',
      vercode: '',
      showMailCode: false,
      showSendMail: true,
      one: Math.floor(Math.random()*10),
      two: Math.floor(Math.random()*10)
    }
  },
  methods: {
    sendMail: function () {
      sendRegisterMail(this.email).then(this.sendMailSucc)
    },
    sendMailSucc: function () {
      this.showMailCode = true;
      this.showSendMail = false;
      message('邮箱发送成功，请填写到邮箱验证码')
    },
    submitForm: function () {
      if (this.password !== this.confirmPassword) {
        message('两次密码不一样', 'warning')
        return
      }

      if (this.vercode != this.one + this.two) {
        message('人类验证错误', 'warning')
        return
      }

      register(this.email, this.code, this.name, this.password).then(this.registerSucc)
    },
    registerSucc: function (res) {
      if (res) {
        message('恭喜注册成功，请登录')
        this.$router.push({
          path:'/login'
        })
      }
    }
  }

}
</script>
